package cn.tedu.charging.device.service.impl;

import cn.tedu.charging.common.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.device.dao.repository.StationCacheRepository;
import cn.tedu.charging.device.dao.repository.StationCacheRepositoryFactory;
import cn.tedu.charging.device.dao.repository.StationRepository;
import cn.tedu.charging.device.pojo.param.StationNearQueryParam;
import cn.tedu.charging.device.pojo.po.StationCanalPO;
import cn.tedu.charging.device.pojo.po.StationPO;
import cn.tedu.charging.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class DeviceServiceImpl implements DeviceService {


    @Autowired
    private StationCacheRepositoryFactory stationCacheRepositoryFactory;

    @Autowired
    private StationRepository stationRepository;


    @Override
    public StationInfoVO getStationInfo(Integer gunId) {
        StationInfoVO stationInfoVO = new StationInfoVO();
        return stationInfoVO;
    }

    @Override
    public Boolean updateGunStatus(GunStatusUpdateParam param) {
        return true;
    }

    @Override
    public List<StationInfoVO> nearStation(StationNearQueryParam stationNearQueryParam) {
        List<StationPO> stationPOS =
                stationCacheRepositoryFactory.getStationCacheRepository().search(
                stationNearQueryParam.getLng(),
                stationNearQueryParam.getLat(),
                stationNearQueryParam.getDistance()
        );

        List<StationInfoVO> result = new ArrayList<>();
        for (StationPO stationPO : stationPOS) {
            StationInfoVO stationInfoVO = new StationInfoVO();
            BeanUtils.copyProperties(stationPO,stationInfoVO);
            result.add(stationInfoVO);
        }
        return result;
    }

    @Override
    public void saveAllStations2Cache(List<StationPO> allStations) {
        stationCacheRepositoryFactory.getStationCacheRepository().
                saveAllStations(allStations);
    }

    @Override
    public List<StationPO> getAllStationsFromDB() {
        return stationRepository.getAllStations();
    }

    @Override
    public void insert(StationCanalPO stationCanalPO) {
        stationCacheRepositoryFactory.getStationCacheRepository()
                .insert(stationCanalPO);
    }

    @Override
    public void update(StationCanalPO before, StationCanalPO after) {
        stationCacheRepositoryFactory.getStationCacheRepository()
                .update(before,after);
    }

    @Override
    public void delete(StationCanalPO stationCanalPO) {
        stationCacheRepositoryFactory.getStationCacheRepository()
                .delete(stationCanalPO);

    }
}
